/**
 * Created by sxx on 2017/5/3.
 */
import "./index.less";
import { projSts } from "../config.js";

import bidListTpl from "./apply_list.handlebars";
import paginationTpl from "./paging.handlebars";

let token = sessionStorage.getItem('token');

var ApplyList = {
    pageNo: 1,
    pageSize: 20,
    maxPage: 1,
    isFirstPage: false,
    isLastPage: false,
    total: 0,
    init: function() {
        this.getData()
            .then(this.renderList)
            .then(this.renderPagination)
            .then(this.bindEvent);
    },
    getData: function() {
        return new Promise((resolve, reject) => {
            let self = this;
            let data = {
                "splrSts": "01",
                "pageNo": this.pageNo,
                "pageSize": this.pageSize
            };
            $.ajax({
                url: HOST_ADDR + `/cpmp/splrMng/splrListByStatus/`,
                type: 'get',
                dataType: 'json',
                headers: {
                    'access_token': token,
                    'login_type': 'ums',
                    "splrStatus": "02",
                    "pageNo": this.pageNo,
                    "pageSize": this.pageSize
                },
                // data:JSON.stringify(data),
                contentType: "application/json",
                success: function(resp) {
                    if (resp.success) {
                        console.log(resp);
                        resolve(resp);
                    } else {
                        alert(resp.msgInf);
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });


        });

    },
    // 渲染列表

    renderList: function(resp) {
        let self = ApplyList;
        // 处理数据begin
        self.total = resp.data.count;
        self.isFirstPage = self.pageNo === 1;
        self.maxPage = Math.ceil(self.total / self.pageSize);
        console.log(resp.data);
        $.each(resp.data.list,function(index,item){
            item.applyAvailabel = item.splrSts === "01";
        });
        // 处理数据end
        if (self.pageNo * self.pageSize >= self.total) {
            self.isLastPage = true;
        } else {
            self.isLastPage = false;
        }

        let resultHtml = bidListTpl({ applyList: resp.data.list });
        $('.J_apply-list').html(resultHtml);
    },

    // 渲染分页
    renderPagination: function() {
        let self = ApplyList;
        let pageList = [];
        for (let i = 0; i < self.maxPage; i++) {
            pageList.push({ idx: i + 1 });
        }
        let paginationHtml = paginationTpl({
            begin: (self.pageNo - 1) * self.pageSize + 1,
            end: self.isLastPage ? self.total : self.pageNo * self.pageSize,
            total: self.total,
            isLastPage: self.isLastPage,
            isFirstPage: self.isFirstPage,
            pageList: pageList

        });
        $('.J_pagination').html(paginationHtml);
        $('.page-item').each((index, item) => {
            if ($(item).data('index') === self.pageNo) {
                $(item).addClass('active');
            }
        });
    },

    // 绑定事件
    bindEvent: function() {
        let self = ApplyList;
        $('.J_prev').unbind().click(function() {
            if (self.pageNo > 1) {
                self.pageNo = self.pageNo - 1;
                self.getData()
                    .then(self.renderList)
                    .then(self.renderPagination)
                    .then(self.bindEvent);
            }
        });
        $('.J_next').unbind().click(function() {
            console.log('J_next');

            if (self.pageNo < self.maxPage) {
                console.log('aaa');
                self.pageNo = self.pageNo + 1;
                self.getData()
                    .then(self.renderList)
                    .then(self.renderPagination)
                    .then(self.bindEvent);
            }

        });
        $('.page-item').off().on('click', function() {
            let $item = $(this);
            console.log('aaaa');
            if (!$item.hasClass('active')) {
                self.pageNo = $item.data('index');

                self.getData()
                    .then(self.renderList)
                    .then(self.renderPagination)
                    .then(self.bindEvent);
            }
        });
    },
};

App.init();
ApplyList.init();
